package com.sec.android.app.myfiles.widget.listview;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.local.foldertree.FolderTreeRecord;
import com.sec.android.app.myfiles.util.PreferenceUtils;

public class FolderTreeListViewImp extends AbsListViewImp {

    public FolderTreeListViewImp(Context context, View parentView) {
        super(context, parentView);
    }

    @Override
    protected int _getListLayoutId() {
        return R.id.folder_tree_list;
    }

    @Override
    protected int _getItemLayoutId() {
        return R.layout.folder_tree_list_item;
    }

    public void checkFolderTreeOpen(int position) {
        if (mAdapter != null) {
            if ((position != AppConstants.UNDEFINED) && (mAdapter.getDragPosition() != position)) {
                mAdapter.setDragPosition(position);
                invalidateViews();

                FileRecord record = getFileRecord(position);
                boolean showHidden = PreferenceUtils.getShowHiddenFiles(mContext);

                if (position != AppConstants.UNDEFINED  && record instanceof FolderTreeRecord && !((FolderTreeRecord)record).isOpened() && ((FolderTreeRecord)record).hasChildDirCount(showHidden) > 0) {
                    mAutoExpandByDragHandler.removeMessages(0);
                    Message msg = mAutoExpandByDragHandler.obtainMessage(0);
                    msg.arg1 = position;
                    mAutoExpandByDragHandler.sendMessageDelayed(msg, AppConstants.DRAG_AND_DROP_FOLDER_OPEN_DELAY_TIME);
                }
            }
        }
    }

    private Handler mAutoExpandByDragHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            mAdapter.toggleExpand(msg.arg1);
            return false;
        }
    });
}
